ARCH = arm
BOARD = imx6ull
CROSS_COMPILE = arm-linux-gnueabihf-
AS		= $(CROSS_COMPILE)as
LD		= $(CROSS_COMPILE)ld
CC		= $(CROSS_COMPILE)gcc
CPP		= $(CC) -E
AR		= $(CROSS_COMPILE)ar
NM		= $(CROSS_COMPILE)nm

STRIP		= $(CROSS_COMPILE)strip
OBJCOPY		= $(CROSS_COMPILE)objcopy
OBJDUMP		= $(CROSS_COMPILE)objdump

CFLAGS := -Wa,-mimplicit-it=thumb -Wall -nostdlib -fno-builtin
CFLAGS += -I $(shell pwd)/include
CFLAGS += -I $(shell pwd)/arch/$(ARCH)/$(BOARD)/include

SFLAGS := -Wall -nostdlib -fno-builtin

LDFLAGS := -Tscript/imx6ull.lds
LDFLAGS += -lgcc -L /home/book/share/100ask_imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/6.2.1

TOPDIR := $(shell pwd)

export AS LD CC CPP AR NM
export STRIP OBJCOPY OBJDUMP
export SFLAGS CFLAGS LDFLAGS
export TOPDIR

obj-y += arch/$(ARCH)/
obj-y += init/
obj-y += kernel/

all : start_recursive_build kernel.imx kernel.dis
	cp kernel.bin ~/tftpboot
	@echo kernel.imx is Ready!

start_recursive_build:
	make -C ./ -f $(TOPDIR)/Makefile.build

kernel.elf : built-in.o
	$(LD) -Tscript/imx6ull.lds -o $@ $< $(LDFLAGS)

kernel.dis : kernel.elf
	$(OBJDUMP) -D -m arm $< > $@

kernel.bin : kernel.elf
	$(OBJCOPY) -O binary -S $< $@

kernel.imx : kernel.bin
	$(TOPDIR)/tools/mkimage -n $(TOPDIR)/tools/imximage.cfg.cfgtmp -T imximage -e 0x80100000 -d $< $@

clean:
	rm -f $(shell find -name "*.o")
	rm -f kernel.*

distclean:
	rm -f $(shell find -name "*.o")
	rm -f $(shell find -name "*.d")
	rm -f kernel.*